/** Author: JTrapp@Google.com
* (c) 07.01.2008 */

var spreadsheetsBaseURL = "http://spreadsheets.google.com/";
  
function resetMap(){ // with GData support
         //_gel("SF_div").innerHTML="<div id=\"mapDiv\" class=\"map\">"+prefs.getMsg("loadingMap")+"</div><br/><div id=\"status\" class=\"status\"><div>";
        map = new google.maps.Map2(_gel("SF_map"));
        map.setCenter(new google.maps.LatLng(41.875696,-87.624207), 3); 
        geocoder = new google.maps.ClientGeocoder();        
        createIcon(iconURL);

        var len = requestURL.length;
        var suffix = requestURL.substring(len-4, len);
        if ((suffix == ".csv")||(suffix == ".txt")){
            readCSV(requestURL);
        }else if (requestURL.substring(0,spreadsheetsBaseURL.length) == spreadsheetsBaseURL){
            readSpreadsheetGData(requestURL);
        }
        initMapControls();
        loaded = true;
}




function readSpreadsheetGData(requestURL){
    _IG_FetchXmlContent(requestURL, function (response) {  
        //google.maps.Log.write( "Spreadsheet"+response.getElementsByTagName("title").item(0).nodeValue);
        _IG_SetTitle(response.getElementsByTagName("title").item(0).nodeValue);
        var bounds = new google.maps.LatLngBounds( );    
        var entries = response.getElementsByTagName("entry");
        if (entries!=null){
            if (adminMode){
                google.maps.Log.write( "Geocoding"+entries.length+" addresses.");
            }
            for (var i = 0; i < entries.length ; i++) { 
                // For each <food> node, get child nodes.
                createMarkerSpreadsheetGData(entries.item(i), bounds, i, printMarker);
            }
            map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds)); 
        } else if (adminMode){
            google.maps.Log.writeHtml( "<a href=\""+requestURL+"\">Illegal format in shoplist.</a>");
            google.maps.Log.write(response);
        }
    },10 );
}
*/
/**
* Select fields from the entry to create the marker.
*/
function createMarkerSpreadsheetGData(entry, bounds, rowNr, callback) {
    var values = new Array();
    for (var i=0; i<gDataLabels.length; i++){
        values[i] = getNodeValue(entry, gDataLabels[i]);
    }
    createMarker(gDataLabels,values, bounds, rowNr, callback);
}

    
   
/**
* Retrieve the first child element for a given tag.
* this method will only look for direct childs. No grandchilds will be found.
* For some an unknown reason, I didn't get the standard getElementsByTagNameNS 
* to work. If you know how, please substitute and let me know.

* @param {Element} element Some Dom element
* @param {String} tagname The child elements tag name, which may include the namespace prefix.
* @return {Node}Child element or the String "[<"+tagname+"> null]" or "[<"+tagname+"> not found]". 
*/
function getNodeValue(element, tagname) {
    var nodeList = element.childNodes;
    for (var i = 0, node; node = nodeList[i]; i++) {
        if (node.nodeName == tagname) {
            if (node.firstChild!=null){
                return node.firstChild.nodeValue;
            } else {
                return "";
            }
        } else {
            var result = getNodeValue(node, tagname);
            if (result!=null) {
                return result;
            }
        }
    }
    return null;
}

